﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
}
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>工单添加</title>
	@* 	<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/layui-src/dist/css/layui.css"> *@
	<link href="//unpkg.com/layui@2.9.20/dist/css/layui.css" rel="stylesheet">

</head>

<body>
	<div class="layui-container">
		<form class="layui-form" action="" lay-filter="workOrderForm">
			<!-- 第一行 -->
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">工单编号</label>
					<div class="layui-input-inline">
						<input type="text" name="orderNumber"  placeholder="工单编号:不输入默认生成" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">产品</label>
					<div class="layui-input-inline">
						<select lay-submit lay-filter="aihao" id="chanpinName">
							<option value=0>请选择产品</option>
						</select>
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">产品名称</label>
					<div class="layui-input-inline">
						<input type="text" name="productName" required lay-verify="required" placeholder="请输入产品名称" autocomplete="off" class="layui-input">
					</div>
				</div>
			</div>
			<!-- 第二行 -->
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">产品规格</label>
					<div class="layui-input-inline">
						<input type="text" name="productSpecification" placeholder="请输入产品规格" autocomplete="off" class="layui-input" disabled>
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">产品编号</label>
					<div class="layui-input-inline">
						<input type="text" name="productCode" placeholder="请输入产品编号" autocomplete="off" class="layui-input" disabled>
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">单位</label>
					<div class="layui-input-inline">
						<input type="text" name="unit" placeholder="请输入单位" autocomplete="off" class="layui-input" disabled>
					</div>
				</div>
			</div>
			<!-- 第三行 -->
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">计划数</label>
					<div class="layui-input-inline">
						<input type="number" name="planQuantity" required lay-verify="required" placeholder="请输入计划数" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">计划开始时间</label>
					<div class="layui-input-inline">
						<input type="text" name="planStartTime" required lay-verify="required" placeholder="请输入计划开始时间" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">计划结束时间</label>
					<div class="layui-input-inline">
						<input type="text" name="planEndTime" required lay-verify="required" placeholder="请输入计划结束时间" autocomplete="off" class="layui-input">
					</div>
				</div>
			</div>
			<!-- 第四行 -->
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">状态</label>
					<div class="layui-input-inline">
						<input type="radio" name="status" value="1" checked title="未开始" disabled>
						<input type="radio" name="status" value="2" title="进行中" disabled>
						<input type="radio" name="status" value="3" title="已完成" disabled>
						<input type="radio" name="status" value="4" title="已撤回" disabled>
						<input type="radio" name="status" value="5" title="已取消" disabled>
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">备注</label>
					<div class="layui-input-inline">
						<textarea name="remarks" placeholder="请输入备注" class="layui-textarea"></textarea>
					</div>
				</div>
			</div>
			<!-- 提交和重置按钮 -->
			<div class="layui-form-item">
				<div class="layui-input-block">
					<button class="layui-btn" lay-submit lay-filter="Add">提交</button>
					<button type="reset" class="layui-btn layui-btn-primary">重置</button>
				</div>
			</div>
		</form>
	</div>
	任务
	<table class="layui-hide" id="ID-table-demo-templet"></table>
	<script type="text/html" id="toolbarDemo">
		<button class="layui-btn layui-btn-sm" lay-on="test-alert-btn1" >+选择产品</button>
		<button class="layui-btn layui-btn-sm" >删除行</button>
		<button class="layui-btn layui-btn-sm" >导入</button>
		<button class="layui-btn layui-btn-sm" >刷新</button>
	</script>
	<script src="//unpkg.com/layui@2.9.20/dist/layui.js"></script>
	<script>
		layui.use(['table', 'layer', 'form', 'laydate'], function () {

			var form = layui.form;
			var laydate = layui.laydate;
			var table = layui.table;
			var layer = layui.layer;
			var mockData = JSON.parse(localStorage.getItem("WorksheetsData"));//数据源
			var $ = layui.$;



			// 获取今天的日期
			var today = new Date();
			var startOfDay = today.getFullYear() + '-' + (today.getMonth() + 1).toString().padStart(2, '0') + '-' + today.getDate().toString().padStart(2, '0') + 'T' + '00:00:00';
			var endOfDay = today.getFullYear() + '-' + (today.getMonth() + 1).toString().padStart(2, '0') + '-' + today.getDate().toString().padStart(2, '0') + 'T' + '23:59:59';
			// 初始化日期选择器
			laydate.render({
				elem: 'input[name=planStartTime]', // 指定元素
				type: 'datetime',
				value: startOfDay // 设置默认值为当天开始时间
			});
			laydate.render({
				elem: 'input[name=planEndTime]', // 指定元素
				type: 'datetime',
				value: endOfDay // 设置默认值为当天结束时间
			});
			// 监听select的变化
			form.on('select(aihao)', function (data) {
				GetproductList();
			});


			// 渲染
			table.render({
				elem: '#ID-table-demo-templet',
				data: mockData,
				page: true,
				cols: [[
					{ type: 'checkbox', fixed: 'left' },
					{ field: 'processName', width: 180, title: '工序名称', },
					{ field: 'processNumber', width: 180, title: '工序编号', },
					{ field: 'adverseList', width: 180, title: '不良品列表', },
					{ field: 'reportingAuthority', width: 180, title: '报工权限', },
					{ field: 'matching', width: 180, title: '报工数配比', },
					{ field: 'scheduledStartTime', width: 180, title: '计划开始时间', },
					{ field: 'scheduledEndTime', width: 180, title: '计划结束时间', },
					{ field: 'numberofpackages', width: 180, title: '计划数', },
					{ field: 'goodScore', width: 180, title: '良品数', },
					{ field: 'defectiveQuantity', width: 180, title: '不良品数', },
					{ field: 'actualStartTime', width: 180, title: '实际开始时间', },
					{ field: 'actualEndTime', width: 180, title: '实际结束时间', },
					{ field: 'createId', width: 180, title: '创建人', },
					{ field: 'createDate', width: 180, title: '创建时间', },
					{ field: 'updateId', width: 180, title: '修改人', },
					{ field: 'updateDate', width: 180, title: '修改时间', }
				]],
				toolbar: '#toolbarDemo',
				initSort: { // 设置初始排序
					field: 'processNumber', // 字段名
					type: 'desc' // 倒序
				},
				height: 366
			});

			//  realQuantity实际数量', State状态 WorkSheetNumber单号


			function Add() {
				console.log("执行保存操作");
				var planProductDetails = [];
				var checkedCheckboxes = $("table.layui-table tbody input[type='checkbox'][name='layTableCheckbox']:checked");
				var count = checkedCheckboxes.length;
				count = count / 2;
				$("table.layui-table tbody input[type='checkbox'][name='layTableCheckbox']:checked").each(function () {
					var row = $(this).closest('tr');
					var rowData = {
						"createId": 1,
						"createName": "小罗",
						"createDate": new Date().toISOString(),
						"updateId": 0,
						"updateName": "",
						"updateDate": null,
						"deleteId": 0,
						"deleteName": "",
						"deleteDate": false,
						"processNumber": row.find('td').eq(2).text(),
						"workSheetNumber": "",
						"processName": row.find('td').eq(1).text(),
						"reportingAuthority": row.find('td').eq(5).text(),
						"matching": row.find('td').eq(5).text(),
						"adverseList": row.find('td').eq(3).text(),
						"scheduledStartTime": $("input[name=planStartTime]").val(),
						"scheduledEndTime": $("[name=planEndTime]").val(),
						"numberofpackages": $("input[name=planQuantity]").val(),
						"realQuantity": 0,
						"goodScore": 0,
						"defectiveQuantity": 0,
						"actualTime": 0,
						"actualStartTime": null,
						"actualEndTime": null,
						"state": 1
					};
					planProductDetails.push(rowData);
				});
				var firstTwoDetails = planProductDetails.slice(0, count)
				console.log("选中的行数据：", firstTwoDetails);


				var workSheetss = {
					'createId': 1, //
					'createName': "小罗", //
					'createDate': new Date().toISOString(), //
					'updateId': null, //
					'updateName': "", //
					'updateDate': null, //
					'deleteId': null, //
					'deleteName': "", //
					'deleteDate': false, //
					"workSheetNumber": "",
					"productNumber": $("#chanpinName").val(), // 获取下拉框的值,
					"numberofpackages": $("input[name=planQuantity]").val(),
					"scheduledStartTime": $("[name=planStartTime]").val(),
					"scheduledEndTime": $("[name=planEndTime]").val(),
					"startTime": null,
					"endTime": null,
					"state": 1,
					"remark": $("[name=remarks]").val(),
					"taskIds": $("#chanpinName").val(),
					"productRelatedDocuments": "",
				}

				var data = {
					workSheets: workSheetss,
					taskTable: firstTwoDetails
				}
				console.log("选中的行数据：", JSON.stringify(data));
				$.ajax({
					url: '@ViewBag.Keep' + "/api/Department/workSheetCreate",
					type: 'POST',
					data: JSON.stringify(data),
					contentType: 'application/json',
					success: function (result) {
						if (result.code == 0) {
							layer.msg(result.messege);
							console.log(window.name)
							// 关闭当前layer弹窗
						    layer.close(layer.getFrameIndex(window.name));
							// 刷新父页面
					       window.parent.location.reload();
						} else {
							layer.msg(result.messege);
						}
					},
					error: function (xhr, status, error) {
						console.log(xhr.responseText);
					}
				});

			}


			//弹框
			layui.use(function () {
				var layer = layui.layer;
				var util = layui.util;
				// 事件
				util.on('lay-on', {
					'test-alert-btn1': function () {
						layer.open({
							type: 2,
							area: ['1300px', '600px'],
							shadeClose: true,
							title: '选择工序',
							content: '/Worksheets/WorksheetShow',
						});
					}
				});
			});
			// 监听表格之外的按钮点击事件
			$('.layui-btn[lay-filter="Add"]').click(function () {
			debugger
			
				// 判断是否选中数组
				if ($('input[name="layTableCheckbox"]:checked').length === 0) {
					layer.msg('请选择要操作的数据');
					return;
				}
				// 执行保存操作
				Add();; // 假设这是您定义的函数，用于处理选中的数据
			});



			// 获取产品列表
			productList()
			function productList() {
				$.ajax({
					url: '@ViewBag.Keep' + '/api/Department/ProductSearchSelect',
					type: 'get',
					dataType: 'json',
					success: function (res) {
						// 请求成功后，将返回的数据填充到第一个下拉选择框中
						$.each(res.data, function () {
							$("#chanpinName").append(`<option value="${this.productId}">${this.productName}</option>`);

						});
						// 只在数据加载完成后渲染表单元素
						form.render();
					}
				});
			}
			// 获取产品信息反填
			function GetproductList() {
				var productId = $('#chanpinName').val();
				console.log(productId);
				console.log(1);
				$.ajax({
					url: '@ViewBag.Keep' + '/api/Department/ProductSearchSelect?ProductId=' + productId,
					type: 'get',
					dataType: 'json',
					success: function (res) {
						// 请求成功后，将返回的数据填充到第一个下拉选择框中
						$.each(res.data, function () {
							$("[name='productName']").val(this.productName);
							$("[name='productCode']").val(this.productNumber);
							$("[name='productSpecification']").val(this.productSpecification);
							$("[name='unit']").val(this.stockUnit);

						});
						// 只在数据加载完成后渲染表单元素
						form.render();
					}
				});
			}

			// 底部
		});


	</script>
</body>
</html>
